package com.neu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.pojo.Fund;
import com.neu.pojo.User;
import com.neu.service.UserService;
import com.neu.utils.HttpClientUtil;
import com.neu.utils.UserConstantInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String,String> sessionRedisTemplate;
    @PostMapping("/login")
    public Map<String,String> login(@RequestParam("code") String code){
        //获取openid与session_key
        Map<String, String> param = new HashMap<>();
        param.put("appid", UserConstantInterface.WX_LOGIN_APPID);
        param.put("secret", UserConstantInterface.WX_LOGIN_SECRET);
        param.put("js_code", code);
        param.put("grant_type", UserConstantInterface.WX_LOGIN_GRANT_TYPE);
        String json= HttpClientUtil.doGet(UserConstantInterface.WX_LOGIN_URL, param);
        ObjectMapper mapper=new ObjectMapper();
        Map<String,String> map=new HashMap<>();
        try {
            map=mapper.readValue(json, new TypeReference<Map<String, String>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        String session_key=map.get("session_key");
        String openid=map.get("openid");
        System.out.printf("用户登录，openid:%s  session_key:%s",openid,session_key);

        //更新Redis中的session_key
        String oldKey=userService.getSessionKey(openid);
        if(oldKey!=null&&!oldKey.equals(session_key)) sessionRedisTemplate.delete(oldKey);
        sessionRedisTemplate.opsForValue().set(session_key,openid);
        //更新数据库
        Map<String,String> result=new HashMap<>();
        result.put("session_key",session_key);
        result.put("openid",openid);
        User user=userService.getUserById(openid);
        if(user==null){
            user=new User(openid,session_key);
            userService.addUser(user);
        }
        else userService.updateSessionKey(openid,session_key);
        return result;
    }
    @PostMapping("/getFavouriteFund")
    public List<Fund> getFavouriteFund(@RequestParam("session_key") String session_key){
        String openid=sessionRedisTemplate.opsForValue().get(session_key);
        return userService.getFavouriteFund(openid);
    }

    @PostMapping("/addFavouriteFund")
    public int addFavouriteFund(@RequestParam("session_key") String session_key,@RequestParam("code") String code){
        String openid=sessionRedisTemplate.opsForValue().get(session_key);
        return userService.addFavouriteFund(openid,code);
    }

    @PostMapping("/deleteFavouriteFund")
    public int deleteFavouriteFund(@RequestParam("session_key") String session_key,@RequestParam("list") List<Fund> list){
        String openid=sessionRedisTemplate.opsForValue().get(session_key);
        int result=0;
        for(Fund fund:list){
            result+=userService.deleteFavouriteFund(openid,fund.getCode());
        }
        return result;
    }

    @PostMapping("/updatePreference")
    public int updatePreference(@RequestParam("session_key") String session_key,@RequestParam("preference") int preference){
        String openid=sessionRedisTemplate.opsForValue().get(session_key);
        System.out.println("openid:"+openid+" 投资偏好已改为："+preference);
        return userService.updatePreference(openid,preference);
    }

}
